#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Views for the afcp app.
#
# Created on 2008-10-28.
# $Id: views.py 41 2010-07-21 10:33:30Z guolin.mobi $
#

import logging
import datetime
import random
import urllib

from django.template import loader
from django.template import Context
from django.http import HttpRequest
from django.http import HttpResponse


from afcpweb.apps.misc.views import not_found
from afcpweb.apps.common.actions import Language 
from afcpweb.apps.common.actions import get_common_data

from afcpweb.apps.common import utils
from afcpweb.apps.afcp import resources

from afcpweb.apps.afcp import planet
from afcpweb.apps.afcp import news_feed

from afcpweb.apps.afcp.config import Config


def home(request):
    "Displays the AFCP home page."
    
    ctxt_dict = { "feeds"         : news_feed.FEEDS,
                  "banner_image"  : random.choice(resources.BANNER_IMAGES), }
    ctxt_dict.update(get_common_data(request))
    ctxt = Context(ctxt_dict)
    tmpl = loader.get_template("%s/home.html" % Config.APP_NAME)
    return HttpResponse(tmpl.render(ctxt))


def contact(request, lang=None):
    "Displays the requested static contact page in a requested or the current language."
    
    if lang is None:
        lang = Language.SUPPORTED_LANG_CODES
    if lang not in Language.SUPPORTED_LANG_CODES:
        lang = Language.DEFAULT_LANG_CODE
    
    ctxt_dict = { "page_name" : "contact",
                  "lang_code" :lang,}
                
    ctxt_dict.update(get_common_data(request))
    ctxt = Context(ctxt_dict)
    tmpl = loader.get_template("%s/static/%s/contact/contact.html" % (Config.APP_NAME, lang ) )
    return HttpResponse(tmpl.render(ctxt))


def about(request, name=None, lang=None):
    "Displays the requested static about page in a requested or the current language."
    
    STATIC_PAGES = ( "nous", "paristech", "paristechchine" , "activities", "bureau")
    
    if name is None:
        name = "nous"
    if name not in STATIC_PAGES:
        not_found(request)
    
    if lang is None:
        lang = Language.get_lang(request)
    if lang not in Language.SUPPORTED_LANG_CODES:
        lang = Language.DEFAULT_LANG_CODE
        
    ctxt_dict = { "page_name"    : name,
                  "lang_code"    : lang, }
    ctxt_dict.update(get_common_data(request))
    ctxt = Context(ctxt_dict)
    tmpl = loader.get_template("%s/static/fr/about/%s.html" % (Config.APP_NAME, name ))
    return HttpResponse(tmpl.render(ctxt))


def news(request):
    "Displays the planet AFCP page (a feeds aggregator)."
    
    ctxt_dict = { "feeds"       : news_feed.FEEDS,
                  "page_name" : "news",
                  }
    ctxt_dict.update(get_common_data(request))
    ctxt = Context(ctxt_dict)
    tmpl = loader.get_template("%s/news.html" % Config.APP_NAME)
    return HttpResponse(tmpl.render(ctxt))


def amusement(request, name=None):
    "Displays the requested amusement page in a requested or the current language."
    
    if name is None:
        name = "planet"
    
    if name == "planet":
        return planet_afcp(request)
    elif name == "photos":
        return photos_afcp(request)
    else:
        return not_found(request)


def planet_afcp(request):
    "Displays the planet AFCP page (a feeds aggregator)."
    
    ctxt_dict = { "feeds"       : planet.FEEDS,
                  "page_name" : "planet",
                  }
    ctxt_dict.update(get_common_data(request))
    ctxt = Context(ctxt_dict)
    tmpl = loader.get_template("%s/amusement/planet.html" % Config.APP_NAME)
    return HttpResponse(tmpl.render(ctxt))


def photos_afcp(request):
    "Displays the photos AFCP page (a feeds aggregator)."
    
    ctxt_dict = { "feeds"       : planet.PHOTOS,
                  "page_name" : "photos",
                  }
    ctxt_dict.update(get_common_data(request))
    ctxt = Context(ctxt_dict)
    tmpl = loader.get_template("%s/amusement/photos.html" % Config.APP_NAME)
    return HttpResponse(tmpl.render(ctxt))
 

#